$(function () {
    $('#xy_tcfs').combobox({
        url: $.webPath() + '/base/sjzdsy/get_sjzdz?bm=tcfs',
        valueField: 'id',
        textField: 'z',
        multiple: false,
        editable: true,
        panelHeight: '100',
        onChange: function (newValue, oldValue) {
            if (newValue != '') {
                $('#tcgs_color').css({color: "#f45212"});
                $('#tcbm_color').css({color: "#f45212"});
                $('#xy_jyms_tcgs_id').combotree({
                    required: true
                });
                $('#xy_tcbm').combobox({
                    required: true
                });
            } else {
                $('#tcgs_color').css({color: "#485F71"});
                $('#tcbm_color').css({color: "#485F71"});
                $('#xy_jyms_tcgs_id').combotree({
                    required: false
                });
                $('#xy_tcbm').combobox({
                    required: false
                });
            }
        }
    });

    $('#xy_tcbm').combobox({
        //过滤下拉列表
        filter: function (q, row) {
            if (q == "") {
                return true;
            }
            var opts = $(this).combobox('options');
            return row[opts.textField].indexOf(q) >= 0;
        }
    });
    //统筹公司对统筹部门的级联操作
    $("#xy_jyms_tcgs_id").jgbmtrees($("#xy_tcbm"), undefined, $("#xy_tcbm").val(), undefined);

//    $("#xy_dbf_id").qyjgcomboboxforsearch({
//        onLoadSuccess: function () {
//            if ($("#dbf_id").val()) {
//                $(this).combobox('setValue', $("#dbf_id").val());
//                $(this).combobox('setText', $("#dbf_jc").val());
//            }
//        }
//    });
    $("#xy_dbf_id").qyjgzcombobox({
        onSuccess : function(){
            $("#xy_dbf_id").zcombobox("setMap",{ id:$("#dbf_id").val(),jc:$("#dbf_jc").val()})
        }
    });

    $("#xy-uploadify").upload('fileQueue', {
        onUploadSuccess: function (file, data, response) {
            var obj = eval('(' + data + ')');
            alert(obj);
            $("#fileQueue .data").remove();
            $("#fileQueue .uploadify-progress").fadeOut(500, function () {
                $(this).remove();
            });
            $("#" + file.id).append("<input type='hidden' id='file" + obj.fileId + "' name='xy.fjList.id' value='" + obj.fileId + "'/>");
            $("#" + file.id + " .cancel a").bind('click', function () {
                $(this).attr('href', 'javascript:void(0)');
                deleteFile(obj.fileId, file.id);
            });
        }
    });


    $("#xy_nxyl").numberbox({
        min: 0,
        max: 9999999,
        precision: 0
    })

    $("#xy_yxyl").numberbox({
        min: 0,
        max: 999999999999,
        precision: 0
    })

    $("#xy_dbe").numberbox({
        min: 0,
        max: 999999999999,
        precision: 4,
        groupSeparator: ","
    })

    $("#xy_nxyl").change(function () {
        $("#xy_yxyl").numberbox('setValue', $("#xy_nxyl").val() / 12);
    })

    $("#xy_yxyl").change(function () {
        $("#xy_nxyl").numberbox('setValue', $("#xy_yxyl").val() * 12);
    })

    $("[name='cgxy.gys.id']").combobox({valueField: 'id', textField: 'qc'});//.qyjgcombobox(false);

    $("[name='sfxy.gys.id']").combobox({valueField: 'id', textField: 'qc'});//.qyjgcombobox(false);

    $("[name='xsxy.kh.id']").combobox({valueField: 'id', textField: 'qc'});//.qyjgcombobox(false);

    $("[name='sfxy.kh.id']").combobox({valueField: 'id', textField: 'qc'});//.qyjgcombobox(false);

    //付款方式-加载
    $("[name='xy.fkfs.id']").sjzdcombobox("fkfs");
    //回款计划-加载
    $("#xsxy_hkjsfs_id").sjzdcombobox("khjsfs");
    $("#sfxy_hkjsfs_id").sjzdcombobox("khjsfs");


    content($("[name='type']:checked").val());
    ljCombo($("[name='type']:checked").val());
    $("#xy_lj_id").combobox('reload', $.webPath() + '/organ/ljwh/ljinfo?queryLj.jyjg=' + $("#xy_nbgs_id").val() + "&queryLj.lx=0,1,2" + "&queryLj.sfkjxy=true"+ "&queryLj.jybm=" + $("#xy_zgjgId").val()||'');

})

function content(type) {
    if (type == 1) {
        $("#hktext").css('display', 'none');
        $("#sfxy_gys").css('display', 'none');
        $("#cgxy_gys").css('display', '');
        $("#gys_head").css('display', '');
        $("#sf_rzlx").css('display', 'none');
        $("#xy_rzlx").combobox({required: false});
        $("#xy_tcfs_head").css('display', '');
    } else if (type == 2) {
        $("#hktext").css('display', '');
        $("#gys_head").css('display', 'none');
        $("#cgxy_gys").css('display', 'none');
        $("#sfxy_gys").css('display', 'none');
        $("#xsxy_kh").css('display', '');
        $("#xsxy_hkjsfs").css('display', '');
        $("#xsxy_hkqx").css('display', '');
        $("#sfxy_kh").css('display', 'none');
        $("#sfxy_hkjsfs").css('display', 'none');
        $("#sfxy_hkqx").css('display', 'none');
        $("#sf_rzlx").css('display', 'none');
        $("#xy_rzlx").combobox({required: false});
        $("#xy_tcfs_head").css('display', 'none');
    } else if (type == 3) {
        $("#hktext").css('display', '');
        $("#gys_head").css('display', '');
        $("#cgxy_gys").css('display', 'none');
        $("#sfxy_gys").css('display', '');
        $("#xsxy_kh").css('display', 'none');
        $("#xsxy_hkjsfs").css('display', 'none');
        $("#xsxy_hkqx").css('display', 'none');
        $("#sfxy_kh").css('display', '');
        $("#sfxy_hkjsfs").css('display', '');
        $("#sfxy_hkqx").css('display', '');
        $("#sf_rzlx").css('display', '');
        $("#xy_rzlx").combobox({required: true});
        $("#xy_tcfs_head").css('display', 'none');
    }
}

function ljCombo(type) {
    var option = {
        onLoadSuccess: function () {
            if ($("#xy_lj_value").val()) {
                $(this).combobox('select', $("#xy_lj_value").val());
                $(this).combobox('disable');
            }
        },
        onSelect: function (record) {
            //在新增的时候执行
            if (record.lx == 0) {
                $("#xy_type_id").val(3);
                $("#xy_type_mc").text("三方协议");
                content(3);
            } else {
                $("#xy_type_id").val(record.lx);
                record.lx == 1 ? $("#xy_type_mc").text("采购协议") : $("#xy_type_mc").text("销售协议")
                content(record.lx);
            }
            if (!$("#xy_content_id").val()) {
                clearValue();
            }

            var sydata = [], xydata = [];
            $.post($.webPath() + '/organ/ljwh/ljinfosxy', {'ljId': record.id}, function (data) {
                $(data).each(function (i, item) {
                    if (item.lx == 'sy') {
                        sydata.push(item);
                    } else {
                        xydata.push(item);
                    }
                })

                if ($("#xy_lj_value").val() == '') {
                    if (sydata && sydata[0]) {
                        $("#cgxy_gys_id").loadLocalRegion(sydata, sydata[0].id);
                        $("#sfxy_gys_id").loadLocalRegion(sydata, sydata[0].id);
                    }
                    if (xydata && xydata[0]) {
                        $("#xsxy_kh_id").loadLocalRegion(xydata, xydata[0].id);
                        $("#sfxy_kh_id").loadLocalRegion(xydata, xydata[0].id);
                    }
                } else {
                    $("#cgxy_gys_id").loadLocalRegion(sydata, '');
                    $("#sfxy_gys_id").loadLocalRegion(sydata, '');
                    $("#xsxy_kh_id").loadLocalRegion(xydata, '');
                    $("#sfxy_kh_id").loadLocalRegion(xydata, '');
                }
            })
            $("#xy_nbgs_id").val(record.jyjg_id);
            $("#xy_nbgs_mc").html(record.jyjg_jc);
            $("#xsxy_hkjsfs_id").combobox('setValue', record.jsfs_id);
            $("#sfxy_hkjsfs_id").combobox('setValue', record.jsfs_id);
            $("[name='xsxy.hkqx']").val(record.hkq);
            $("[name='sfxy.hkqx']").val(record.hkq);


//            $("[name='xy.zycp']").val(record.jypz);
//            $("[name='xy.dbfs']").val(record.dbfs);
        }
    }

    $("#xy_lj_id").xyljcombobox($("#xy_nbgs_id").val(), '0,1,2', 'true',$("#xy_zgjgId").val()||'', option);
}

function changelj(value) {
    $("#xy_lj_id").combobox('reload', $.webPath() + '/organ/ljwh/ljinfo?queryLj.jyjg=' + value + "&queryLj.lx=0,1,2");
}

function clearValue() {
    $("#cgxy_gys_id").combobox('clear');
    $("#sfxy_gys_id").combobox('clear');
    $("#xsxy_kh_id").combobox('clear');
    $("#sfxy_kh_id").combobox('clear');
    $("#xsxy_hkjsfs_id").combobox('clear');
    $("#sfxy_hkjsfs_id").combobox('clear');
    $("[name='xy.zycp']").val('');
    $("[name='xy.dbfs']").val('');
    $("[name='xsxy.hkqx']").val('');
    $("[name='sfxy.hkqx']").val('');
}

function deleteFile(fileId, id) {
    $.messager.confirm('确认', '是否确认删除此附件', function (r) {
        if (r) {
            $.post($.webPath() + '/base/fj/deleteFj', {'fjId': fileId}, function (data) {
                var obj = eval('(' + data + ')');
                if (obj.statusCode == '200') {
                    $("#file" + fileId).remove();
                    $("#" + id).fadeOut(500, function () {
                        $(this).remove();
                    });
                }
            });
        }
    })
}
